<!--
*Author：富行天下团队
 
 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *业务请在@/extension/mall/MebCoupon.js此处编写
 -->
<template>
  <div>
    <!-- 制卡 -->
    <VolBox
      :model.sync="model"
      :title="'制卡--' + title"
      :width="700"
      :height="400"
    >
      <VolForm
        ref="myform"
        :label-width="100"
        :loadKey="true"
        :formFields="editFormFields"
        :formRules="editFormOptions"
      ></VolForm>
      <div slot="footer">
        <Button type="error" icon="md-checkmark" @click="submit" :loading="loading"> 保存</Button>
         <Button type="info" icon="md-close" @click="model = false">关闭</Button> 
      </div>
    </VolBox>
  </div>
</template>

<script>
import VolBox from "@/components/basic/VolBox.vue";
import VolForm from "@/components/basic/VolForm.vue";
import VolTable from "@/components/basic/VolTable.vue";
var vueParam = {
  components: {
    VolBox,
    VolForm,
    VolTable,
  },
  data() {
    return {
      model: false,
      editFormFields: {
        card_type: "",
        card_num: "",
        card_count: "",
        rule_type: 0,
        cardno_prefix: "",
        start_no: "",
        random_type: "",
        cardno_suffix: "",
        time_type: 0,
        note: "",
        cardno_gdrq: "",
        card_day: "",
      },
      editFormOptions: [
        [
          {
            title: "卡模板",
            field: "card_type",
            type: "text",
            disabled: true,
          },
          {
            title: "卡号位数",
            field: "card_num",
            type: "number",
            max:12,
            min:5,
            required: true,
          },
        ],
        [
          {
            title: "发卡数量",
            field: "card_count",
            type: "number",
            required: true,
          },
        ],
        [
          {
            title: "卡号规则",
            field: "rule_type",
            type: "radio",
            data: [
              { key: 0, value: "自定义模式" },
              { key: 1, value: "随机模式" },
            ],
          },
          {
            title: "卡前缀",
            field: "cardno_prefix",
            type: "text",
            hidden: false,
          },
        ],
        [
          {
            title: "卡起始号",
            field: "start_no",
            type: "number",
            hidden: false,
            required:true,
          },
          {
            title: "卡后缀",
            field: "cardno_suffix",
            type: "text",
            hidden: false,
          },
        ],
        [
          {
            title: "随机模式",
            field: "random_type",
            type: "select",
            dataKey: "MebCardRules",
            data: [],
            hidden: true,
          },
        ],
        [
          {
            title: "有效期规则",
            field: "time_type",
            type: "radio",
            data: [
              { key: 0, value: "固定区间模式" },
              { key: 1, value: "随机模式" },
            ],
          },
          {
            title: "区间",
            field: "cardno_gdrq",
            type: "datetime",
            range: true,
          },
        ],
        [{ title: "发卡描述", field: "note", type: "textarea", colSize: 12 }],
      ],
      couponZK: {},
      title: "",
      cardDay: "",
      card_day: "",
      cardMakeId: "",
      loading: false,
      NUMSTATUS: false
    };
  },
  watch: {
    "editFormFields.card_type"(val) {
      let params = {
        order: "desc",
        page: 1,
        rows: 100,
        sort: "cardtypeid",
        wheres: `[{\"name\":\"cardtypeid\",\"value\":\"${val}\",\"displayType\":\"text\"}]`,
      };
      this.http.post("/api/Mebcardtype/getPageData", params).then((res) => {
        res.rows.forEach(i => {
          if (i.cardtypeid == val) {
            this.couponZK = i
          }
        })
      });
    },
    "editFormFields.rule_type"(v, n) {
      let cardno_prefix = this.getFormOption("cardno_prefix");
      let start_no = this.getFormOption("start_no");
      let cardno_suffix = this.getFormOption("cardno_suffix");
      let random_type = this.getFormOption("random_type");
      if (v == 0) {
        cardno_prefix.hidden = false;
        start_no.hidden = false;
        cardno_suffix.hidden = false;
        random_type.hidden = true;
        start_no.required=true

      } else {
        cardno_prefix.hidden = true;
        start_no.hidden = true;
        cardno_suffix.hidden = true;
        random_type.hidden = false;
        start_no.required=false
      }
    },
    "editFormFields.time_type"(v, n) {
      let time_type = this.getFormOption("time_type");
      if (v == 0) {
        this.editFormOptions[5][1] = {
          title: "区间",
          field: "cardno_gdrq",
          type: "datetime",
          range: true,
        };
      } else {
        this.editFormOptions[5][1] = {
          title: "",
          field: "card_day",
          type: "textSpan",
          cont: ["开卡生效后", "天有效"],
        };
      }
    },
    "editFormFields.card_day"(v,n) {
      let reg = /^[0-9]*$/ // 数字
      let reg2 = /^\+?[1-9][0-9]*$/   // 大于0整数
      if (!reg.test(v)) {
        this.$Message.error('请输入数字')
      } else {
        if (!reg2.test(v)) {
          this.$Message.error('请输入大于0整数')
        }
      }
    },"editFormFields.cardno_prefix"(v,n){
      // console.log(typeof(v),"v")
      let  calletter= /^[A-Za-z0-9]*$/g
      if(!calletter.test(v)){
        this.$Message.error('请输入大小写字母或者数字作为卡前缀')
      }
    },
    "editFormFields.cardno_suffix"(v,n){
      let  calletter= /^[a-zA-z]*$/g
      if(!calletter.test(v)){
        this.$Message.error('请输入大写或者小写字母作为卡后缀')
      }

    }

  },
  methods: {
    getFormOption(field) {
      let option;
      this.editFormOptions.forEach((x) => {
        x.forEach((item) => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    submit() {
      let card_num = this.editFormFields.card_num;
      let cardno_prefix = this.editFormFields.cardno_prefix;
      let cardno_suffix = this.editFormFields.cardno_suffix;
      let start_no = this.editFormFields.start_no;
      let card_count = this.editFormFields.card_count;
      let  card_type =this.editFormFields.card_type
      if(card_type==""){
        this.$Message.error('请输入卡模板')
        return
      }
   
       if(!cardno_prefix=="" || !cardno_suffix==""){
        console.log(111,cardno_suffix,cardno_prefix)
        console.log(typeof(cardno_suffix),"v")
        let  calletter= /^[A-Za-z0-9]*$/g
      if(!calletter.test(cardno_prefix)){
        this.$Message.error('请输入大小写字母或数字作为卡前缀')
        return
      }
      let  calletter1= /^[a-zA-z]*$/g
      if(!calletter1.test(cardno_suffix)){
        this.$Message.error('请输入大写或者小写字母作为卡后缀')
        return
      }
       }
      // if (this.editFormFields.rule_type == 0||this.editFormFields.rule_type == 1) {
        // if (card_num < 6 || card_num >= 13) {
        //   this.$Message.error("卡号位数不能超过13位或者不能小于6位");
        //   return;
        // } else {
        //   this.interface();
          // if (
          //   card_num >=
          //   cardno_prefix.length +
          //     cardno_suffix.length +
          //     (Number(start_no) + Number(card_count)).toString().length
          // ) {
          //   this.interface();
          // } else {
          //   this.$Message.error("自定义模式卡号规则位数不能大于卡号位数");
          //   return;
          // }
        // }
      // }
      //  else {
         this.interface();
      // }
    },
    interface() {
      let v = JSON.parse(JSON.stringify(this.editFormFields));
      v.card_type = this.couponZK.cardtypeid;
      v.begindate = this.editFormFields.cardno_gdrq[0];
      v.enddate = this.editFormFields.cardno_gdrq[1];
      delete v.cardno_gdrq;
      v.status = 0;
      if (this.editFormFields.time_type == 0) {
        v.card_day = "";
        if (v.begindate == null && v.enddate == null) {
          this.$Message.error('请选择有效期规则区间')
          return
        }
      } else {
        v.begindate = null;
        v.enddate = null;
        v.card_day = this.editFormFields.card_day;
        if (v.card_day == '') {
          this.$Message.error('请输入开卡生效后有效期')
          return
        }
      }
      if (this.title == "新建") {
        let param = {
          mainData: {
            makecard: v,
            cardmodel: this.couponZK,
          },
          detailData: [],
        };
        this.loading = true;
        this.http.post("/api/member_card_make/Add", param).then((res) => {
          if (res.status) {
            this.model = false;
            this.loading = false;
            this.$message.success("制卡成功");
            this.$store.getters.data().viewPageTest.refresh();
            
          } else {
            this.loading = false
            this.$message.error(res.message);
          }
        });
      } else {
        v.id = this.cardMakeId;
        let param = {
          mainData: {
            ...v,
          },
        };
        this.loading = true;
        this.http.post("/api/member_card_make/Update", param).then((res) => {
          if (res.status) {
            this.model = false;
            this.loading = false;
            this.$message.success("修改成功");
            this.$store.getters.data().viewPageTest.refresh();
            
          } else {
            this.loading = false
            this.$message.error(res.message);
          }
        });
      }
    },
  },
};
export default vueParam;
</script>
